<?php

/*
 * Kindeditor编辑器类
 */
 
 class Kindeditor extends Base {
 
	//路径
	protected $path = '/assets/js/kindeditor';
	
	//主题，皮肤
	protected $theme = 'default';
	
	//语言
	protected $lang = 'zh_CN';
	
	//编码语言
	protected $encoding = 'UTF-8';
	
	//换行标签，p或br
	protected $new_line_tag = 'p';
	
	//文件上传URL地址
	protected $file_upload_url = '/index.php/kindeditor/upload';
	
	//文件管理URL地址
	protected $file_manage_url = '/index.php/kindeditor/manage';
	
	protected $_valid = array(
		'path',
		'theme',
		'lang',
		'encoding',
		'new_line_tag',
		'file_upload_url',
		'file_manage_url'
	);
 
	public function __construct($config = array())
	{
		if ($config)
		{
			$this->config($config);
		}
	}
	
	/*
	 * 初始化
	 * @access	public
	 * @return	boolean
	 */
	public function init()
	{
		return TRUE;
	}
	
	/*
	 * 显示编辑器
	 * @access public
	 * @return boolean
	 */
	public function show($name = 'content', $value = '', $width = 700, $height = 400, $file_manage = TRUE)
	{
		//将'和"转成html实体
		$value = htmlspecialchars($value, ENT_COMPAT, $this->encoding);
		$width = strpos($width, '%') ? $width : ($width.'px');
		$height = strpos($height, '%') ? $height : ($height.'px');
		$file_manage = $file_manage ? 'true' : 'false';
		$html = '<textarea name="'.$name.'" style="width:'.$width.';height:'.$height.';">'.$value.'</textarea>'."\n";
		$html .= '<script type="text/javascript" src="'.$this->path.'/kindeditor-min.js"></script>'."\n";
		$html .= '<script type="text/javascript" src="'.$this->path.'/lang/'.$this->lang.'.js"></script>'."\n";
		$html .= <<<EOT
<script type="text/javascript">
	var editor;
	KindEditor.ready(function(K){
		editor = K.create('textarea[name="{$name}"]', {
			themeType : '{$this->theme}',
			uploadJson : '{$this->file_upload_url}',
			fileManagerJson : '{$this->file_manage_url}',
			allowFileManager : {$file_manage},
			newlineTag : '{$this->new_line_tag}'
		});
	});
</script>
EOT;
		return $html;
	}
	
	public function set_path($path)
	{
		$this->path = rtrim($path, '/');
	}
	
	public function set_new_line_tag($new_line_tag)
	{
		$this->new_line_tag = strtolower($new_line_tag) == 'p' ? 'p' : 'br';
	}
 
 }